#!/bin/sh
set -e

TESTDIR="$(readlink -f "$(dirname "$0")")"
. "$TESTDIR/framework"

setupenvironment
configarchitecture 'native'

insertinstalledpackage 'sysvinit' 'native' '1' 'Essential: yes'

buildsimplenativepackage 'sysvinit' 'native' '2' 'sid' 'Pre-Depends: sysvinit-core | systemd-sysv
Essential: yes'
buildsimplenativepackage 'sysvinit-core' 'native' '2' 'sid'

buildsimplenativepackage 'systemd-sysv' 'native' '2~conflict' 'sid-conflict' 'Conflicts: sysvinit (<< 2)
Breaks: sysvinit-core'

buildsimplenativepackage 'systemd-sysv' 'native' '2~break' 'sid-break' 'Breaks: sysvinit (<< 2), sysvinit-core'

# one more time, with Protected
insertinstalledpackage 'protected-sysvinit' 'native' '1' 'Protected: yes'

buildsimplenativepackage 'protected-sysvinit' 'native' '2' 'sid' 'Pre-Depends: protected-sysvinit-core | protected-systemd-sysv
Protected: yes'
buildsimplenativepackage 'protected-sysvinit-core' 'native' '2' 'sid'

buildsimplenativepackage 'protected-systemd-sysv' 'native' '2~conflict' 'sid-conflict' 'Conflicts: protected-sysvinit (<< 2)
Breaks: protected-sysvinit-core'

buildsimplenativepackage 'protected-systemd-sysv' 'native' '2~break' 'sid-break' 'Breaks: protected-sysvinit (<< 2), protected-sysvinit-core'


setupaptarchive

cp -a rootdir/var/lib/dpkg/status dpkg.status.backup

testforcebreak() {
	package="sid-$1"
	prefix="$2"
	cp -a dpkg.status.backup rootdir/var/lib/dpkg/status
	rm -f rootdir/var/lib/apt/extended_states
	testfailureequal "Reading package lists...
Building dependency tree...
The following additional packages will be installed:
  ${prefix}sysvinit
The following NEW packages will be installed:
  ${prefix}systemd-sysv
The following packages will be upgraded:
  ${prefix}sysvinit
1 upgraded, 1 newly installed, 0 to remove and 1 not upgraded.
E: This installation run will require temporarily removing the essential package ${prefix}sysvinit:$(getarchitecture 'native') due to a Conflicts/Pre-Depends loop. This is often bad, but if you really want to do it, activate the APT::Force-LoopBreak option.
E: Internal Error, Could not early remove ${prefix}sysvinit:$(dpkg --print-architecture) (2)" aptget install ${prefix}systemd-sysv -t "$package" -s
	# ensure that really nothing happens
	testfailure aptget install ${prefix}systemd-sysv -y -t "$package"
	testdpkginstalled "${prefix}sysvinit"
	testdpkgnotinstalled "${prefix}systemd-sysv"

	# with enough force however …
	cp -a dpkg.status.backup rootdir/var/lib/dpkg/status
	testsuccess aptget install ${prefix}systemd-sysv -y -t "$package" -o APT::Force-LoopBreak=1 -o Debug::pkgDpkgPm=1
	cp rootdir/tmp/testsuccess.output apt.output
	if [ "$prefix" = "protected-" ]; then
		testsuccess grep -- "--force-remove-protected --remove ${prefix}sysvinit" apt.output
    else
		testsuccess grep -- "--force-remove-essential --remove ${prefix}sysvinit" apt.output
	fi
	testsuccess aptget install ${prefix}systemd-sysv -y -t "$package" -o APT::Force-LoopBreak=1
	testdpkginstalled "${prefix}sysvinit" "${prefix}systemd-sysv"
}

testnoforcebreak() {
	package="sid-$1"
	prefix="$2"
	cp -a dpkg.status.backup rootdir/var/lib/dpkg/status
	rm -f rootdir/var/lib/apt/extended_states
	testequalor2 "Reading package lists...
Building dependency tree...
The following additional packages will be installed:
  ${prefix}sysvinit
The following NEW packages will be installed:
  ${prefix}systemd-sysv
The following packages will be upgraded:
  ${prefix}sysvinit
1 upgraded, 1 newly installed, 0 to remove and 1 not upgraded.
Inst ${prefix}systemd-sysv (2~$1 sid-$1 [$(getarchitecture 'native')]) [${prefix}systemd-sysv:$(getarchitecture 'native') on ${prefix}sysvinit:$(getarchitecture 'native')] []
Conf ${prefix}systemd-sysv:$(getarchitecture 'native') broken
 Breaks:${prefix}sysvinit:$(getarchitecture 'native')
 []
Inst ${prefix}sysvinit [1] (2 sid [$(getarchitecture 'native')])
Conf ${prefix}sysvinit (2 sid [$(getarchitecture 'native')])
E: Conf Broken ${prefix}systemd-sysv:$(getarchitecture 'native')" "Reading package lists...
Building dependency tree...
The following additional packages will be installed:
  ${prefix}sysvinit
The following NEW packages will be installed:
  ${prefix}systemd-sysv
The following packages will be upgraded:
  ${prefix}sysvinit
1 upgraded, 1 newly installed, 0 to remove and 1 not upgraded.
Remv ${prefix}sysvinit [1]
Inst ${prefix}systemd-sysv (2~conflict sid-conflict [$(getarchitecture 'native')])
Conf ${prefix}systemd-sysv (2~conflict sid-conflict [$(getarchitecture 'native')])
Inst ${prefix}sysvinit [1] (2 sid [$(getarchitecture 'native')])
Conf ${prefix}sysvinit (2 sid [$(getarchitecture 'native')])" aptget install ${prefix}systemd-sysv -t "sid-$1" -s

    # we check with 'real' packages here as the simulation reports a 'Conf broken'
	cp -a dpkg.status.backup rootdir/var/lib/dpkg/status
	testsuccess aptget install ${prefix}systemd-sysv -y -t "$package" -o Debug::pkgDpkgPm=1
	cp rootdir/tmp/testsuccess.output apt.output
	testsuccess grep -- "--unpack --auto-deconfigure" apt.output
	testfailure grep -- "--remove" apt.output
	testfailure grep -- "--force-remove" apt.output
	testsuccess aptget install ${prefix}systemd-sysv -y -t "$package"
	testdpkginstalled "${prefix}sysvinit" "${prefix}systemd-sysv"
}

testforcebreak 'conflict'
testnoforcebreak 'break'

testforcebreak 'conflict' 'protected-'
testnoforcebreak 'break' 'protected-'
